package com.lagou.client;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lagou.service.UserService;
import org.I0Itec.zkclient.ZkClient;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;


public class ClientBootStrap {

   // public static  final String providerName="UserService#sayHello#";

    public static void main(String[] args) throws InterruptedException {



        RpcConsumer rpcConsumer = new RpcConsumer();
        UserService proxy = (UserService) rpcConsumer.createProxy(UserService.class);


        ZkClient zkClient = new ZkClient("192.168.115.129:2181");
        while (true){
            Thread.sleep(3000);
            long startTime=new Date().getTime();

            proxy.sayHello("are you ok?");
            System.out.println("已响应");

            long endTime=new Date().getTime();

            String ip=rpcConsumer.ip;
            String path=rpcConsumer.path;
            int port=rpcConsumer.port;

            JSONObject json=new JSONObject();
            json.put("key",ip+port);
            json.put("ip",ip);
            json.put("port",port);
            json.put("time",new Date().getTime());
            json.put("consume",(endTime-startTime));//结束时间-开始时间  =耗时

            System.out.println("ip:"+ip+"port:"+port);

            // 获取子节点列表
            List<String> children = zkClient.getChildren(path);

            JSONArray objects = new JSONArray();
            for (Object o:children){//获取节点下 所有服务器信息
                Object nodes = zkClient.readData(path+"/"+o.toString());
                if(nodes!=null){
                    JSONObject j=(JSONObject) nodes;
                    objects.add(j);
                }
            }
            //找出最用时最短的
            JSONArray array=sortProxyAndCdn(objects);
            for(Object o:array){
                JSONObject j=(JSONObject) o;
                long time=j.getLong("time");
                long consume=j.getLong("consume");
                if(new Date().getTime()-time>50000){
                    System.out.println("==================服务器已经超过5秒无响应，删除zk信息========"+j);
                   // zkClient.deleteRecursive(path);
                }
            }
            System.out.println("响应最快的是："+array.get(0));

            zkClient.writeData(path+"/"+ip+":"+port,json);












        }




    }



    private static JSONArray sortProxyAndCdn(JSONArray bindArrayResult) {
        List<JSONObject> list = JSONArray.parseArray(bindArrayResult.toJSONString(), JSONObject.class);

        System.out.println("排序前："+list);
        Collections.sort(list, new Comparator<JSONObject>() {

            public int compare(JSONObject o1, JSONObject o2) {
                int a = o1.getInteger("consume");
                int b = o2.getInteger("consume");
                if (a > b) {
                    return 1;
                } else if(a == b) {
                    return 0;
                } else
                    return -1;
            }
        });
        JSONArray jsonArray = JSONArray.parseArray(list.toString());

        return jsonArray;
    }





}
